"""
认证相关的数据模型
"""
from pydantic import BaseModel, EmailStr, Field
from typing import Optional
from datetime import datetime


class UserRegister(BaseModel):
    """用户注册模型"""
    username: str = Field(..., min_length=3, max_length=50, description="用户名")
    password: str = Field(..., min_length=6, max_length=100, description="密码")
    phone: Optional[str] = Field(None, pattern=r'^1[3-9]\d{9}$', description="手机号")
    email: Optional[EmailStr] = Field(None, description="邮箱")
    nickname: str = Field(..., min_length=1, max_length=100, description="昵称")


class UserLogin(BaseModel):
    """用户登录模型（账密）"""
    username: str = Field(..., description="用户名或手机号")
    password: str = Field(..., description="密码")


class LoginByCode(BaseModel):
    """验证码登录模型"""
    phone: str = Field(..., pattern=r'^1[3-9]\d{9}$', description="手机号")
    code: str = Field(..., min_length=4, max_length=6, description="验证码")


class SendCodeRequest(BaseModel):
    """发送验证码请求模型"""
    phone: str = Field(..., pattern=r'^1[3-9]\d{9}$', description="手机号")


class DingtalkLogin(BaseModel):
    """钉钉登录模型"""
    code: str = Field(..., description="钉钉授权码")


class TokenResponse(BaseModel):
    """Token响应模型"""
    access_token: str
    refresh_token: Optional[str] = None  # Refresh Token用于续命
    token_type: str = "bearer"
    user_id: int
    username: str


class RefreshTokenRequest(BaseModel):
    """刷新Token请求模型"""
    refresh_token: str = Field(..., description="Refresh Token")


class RefreshTokenResponse(BaseModel):
    """刷新Token响应模型"""
    access_token: str
    refresh_token: str
    token_type: str = "bearer"


class UserInfo(BaseModel):
    """用户信息模型"""
    id: int
    username: str
    nickname: str
    email: Optional[str] = None
    phone: Optional[str] = None
    avatar_url: Optional[str] = None
    bio: Optional[str] = None
    created_at: datetime
    
    class Config:
        from_attributes = True

